﻿using System.Web.Mvc;
using Limestone;

namespace LimestoneMVC.Controllers
{
    public class PageController : Controller
    {
        private IPageProvider PageProvider { get; set; }

        public PageController(IPageProvider pageProvider)
        {
            PageProvider = pageProvider;
        }

        [HttpGet]
        public ActionResult Index()
        {
            return OpenPage("/");
        }


        public ActionResult OpenPage(string pagePath)
        {
            if (string.IsNullOrEmpty(pagePath))
            {
                pagePath = "/";
            }

            var page = PageProvider.Get(pagePath);

            if (page == null)
            {
                return View("Error");
            }

            var viewName = page.TemplatePath.Replace("/", "");
            if (viewName == "")
                viewName = "start";
            
            return View(viewName, page);
        }
    }
}
